package com.tezign.intelligence.mini.dam.aifamilyconsultant.interceptor;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author wuzhiwei
 * @team N2
 * @owner wuzhiwei
 * @Date 2021/9/27
 */
@Slf4j
public class UserStore {

	private UserStore() {
	}

	private static final ThreadLocal<Long> CONTEXT = new TransmittableThreadLocal<>();

	public static void setUerId(Long userId) {
		CONTEXT.set(userId);
	}

	public static Long getUserId() {
		Long userId = CONTEXT.get();

		if (Objects.nonNull(userId) && userId > 0L) {
			return userId;
		}
		log.error("UserStore.getUserId() userId is null or less than 0");
		throw new RuntimeException("UserStore.getUserId() userId is null or less than 0");
	}

	public static void clear() {
		CONTEXT.remove();
	}

}
